﻿using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Model.SchedulerModels;
using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DevelopTool.Bll.SchedulerBlls
{
    /// <summary>
    /// 定时任务-日志操作
    /// </summary>
    public class SchedulerLogBll
    {
        public SchedulerLogBll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<SchedulerLog>();
                col.EnsureIndex(x => x.SchedulerId);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
                col.EnsureIndex(x => x.Result);
                col.EnsureIndex(x => x.Exception);
            }
            DeleteOverdueLog();
        }


        /// <summary>
        /// 删除过期的定时任务日志
        /// </summary>
        private void DeleteOverdueLog()
        {
            int retainDay = Setting.Scheduler.LogRetain_Time;
            DateTime time = DateTime.Now.AddDays(-retainDay);
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<SchedulerLog>();
                col.Delete(it => it.CreateTime < time);
            }

        }


        /// <summary>
        /// 获取定时任务日志集合
        /// </summary>
        /// <param name="schedulerId"></param>
        /// <returns></returns>
        public List<SchedulerLog> GetList(int schedulerId)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<SchedulerLog>();
                return col.Find(it => it.SchedulerId == schedulerId).ToList();
            }
        }


        /// <summary>
        /// 保存定时任务日志
        /// </summary>
        /// <param name="schedulerLog"></param>
        public void Save(SchedulerLog schedulerLog)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<SchedulerLog>();
                col.Insert(schedulerLog);
            }
        }


        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="schedulerId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        public void DeleteLog(int schedulerId, DateTime? startTime, DateTime? endTime)
        {
            if (schedulerId <= 0) return;

            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<SchedulerLog>();

                var condition = PredicateBuilder.True<SchedulerLog>();

                //按时间过滤
                if (startTime != null && endTime != null)
                {
                    condition = condition.And(it => it.CreateTime < startTime);
                    condition = condition.And(it => it.CreateTime > endTime);
                }

                condition = condition.And(it => it.SchedulerId == schedulerId);

                col.Delete(condition);
            }
        }



    }
}
